﻿@inject Microsoft.Extensions.Localization.IStringLocalizer<NavMenu> _localizer
@using System.Security.Claims

<UserCard />
<MudNavMenu>
    <MudNavLink Href="/" Match="NavLinkMatch.All" Icon="@Icons.Material.Outlined.Home">@_localizer["Home"]</MudNavLink>
    @if (_canViewHangfire)
    {
        <MudNavLink Href="/jobs" Target="_blank" Icon="@Icons.Material.Outlined.Work">
            @_localizer["Hangfire"]
        </MudNavLink>
    }
@*    <MudNavLink Href="https://codewithmukesh.com/blog/blazor-hero-quick-start-guide/" Target="_blank" Icon="@Icons.Material.Outlined.ReadMore">
        @_localizer["Quick Start Guide"]
    </MudNavLink>*@
    <MudNavLink Href="/swagger/index.html" Target="_blank" Icon="@Icons.Material.Outlined.LiveHelp">
        @_localizer["Swagger"]
    </MudNavLink>
    <MudListSubheader Class="mt-2 mb-n2">@_localizer["Personal"]</MudListSubheader>
    @if (_canViewDashboards)
    {
        <MudNavLink Href="/dashboard" Icon="@Icons.Material.Outlined.Dashboard">
            @_localizer["Dashboard"]
        </MudNavLink>
    }
    <MudNavLink Href="/account" Icon="@Icons.Material.Outlined.SupervisorAccount">
        @_localizer["Account"]
    </MudNavLink>
    @if (_canViewAuditTrails)
    {
        <MudNavLink Href="/audit-trails" Icon="@Icons.Material.Outlined.Security">
            @_localizer["Audit Trails"]
        </MudNavLink>
    }
    @if (_canViewDocuments || _canViewDocumentTypes)
    {
        <MudListSubheader Class="mt-2 mb-n2">@_localizer["Document Management"]</MudListSubheader>
        @if (_canViewDocuments)
        {
            <MudNavLink Href="/document-store" Icon="@Icons.Material.Outlined.AttachFile">
                @_localizer["Document Store"]
            </MudNavLink>
        }
        @if (_canViewDocumentTypes)
        {
            <MudNavLink Href="/document-types" Icon="@Icons.Material.Outlined.FileCopy">
                @_localizer["Document Types"]
            </MudNavLink>
        }
    }

    @if (_canViewUsers || _canViewRoles)
    {
        <MudListSubheader Class="mt-2 mb-n2">@_localizer["Administrator"]</MudListSubheader>
        @if (_canViewUsers)
        {
            <MudNavLink Href="/identity/users" Icon="@Icons.Material.Outlined.Person">
                @_localizer["Users"]
            </MudNavLink>
        }
        @if (_canViewRoles)
        {
            <MudNavLink Href="/identity/roles" Icon="@Icons.Material.Outlined.Person">@_localizer["Roles"]</MudNavLink>
        }
    }
    @if (_canViewChat)
    {
        <MudListSubheader Class="mt-2 mb-n2">@_localizer["Communication"]</MudListSubheader>
        <MudNavLink Href="/chat" Icon="@Icons.Material.Outlined.Chat">
            @_localizer["Chat"]
        </MudNavLink>
    }
    @if (_canViewProducts || _canViewBrands)
    {
        <MudListSubheader Class="mt-2 mb-n2">@_localizer["Catalog Management"]</MudListSubheader>
        @if (_canViewProducts)
        {
            <MudNavLink Href="/catalog/products" Icon="@Icons.Material.Outlined.CallToAction">
                @_localizer["Products"]
            </MudNavLink>
        }
        @if (_canViewBrands)
        {
            <MudNavLink Href="/catalog/brands" Icon="@Icons.Material.Outlined.CallToAction">
                @_localizer["Brands"]
            </MudNavLink>
        }
    }
</MudNavMenu>

@code {
    private ClaimsPrincipal _authenticationStateProviderUser;

    private bool _canViewHangfire;
    private bool _canViewDashboards;
    private bool _canViewDocuments;
    private bool _canViewDocumentTypes;
    private bool _canViewAuditTrails;
    private bool _canViewRoles;
    private bool _canViewUsers;
    private bool _canViewChat;
    private bool _canViewProducts;
    private bool _canViewBrands;

    protected override async Task OnParametersSetAsync()
    {
        _authenticationStateProviderUser = await _stateProvider.GetAuthenticationStateProviderUserAsync();
        _canViewHangfire = (await _authorizationService.AuthorizeAsync(_authenticationStateProviderUser, Permissions.Hangfire.View)).Succeeded;
        _canViewDashboards = (await _authorizationService.AuthorizeAsync(_authenticationStateProviderUser, Permissions.Dashboards.View)).Succeeded;
        _canViewDocuments = (await _authorizationService.AuthorizeAsync(_authenticationStateProviderUser, Permissions.Documents.View)).Succeeded;
        _canViewDocumentTypes = (await _authorizationService.AuthorizeAsync(_authenticationStateProviderUser, Permissions.DocumentTypes.View)).Succeeded;
        _canViewAuditTrails = (await _authorizationService.AuthorizeAsync(_authenticationStateProviderUser, Permissions.AuditTrails.View)).Succeeded;
        _canViewRoles = (await _authorizationService.AuthorizeAsync(_authenticationStateProviderUser, Permissions.Roles.View)).Succeeded;
        _canViewUsers = (await _authorizationService.AuthorizeAsync(_authenticationStateProviderUser, Permissions.Users.View)).Succeeded;
        _canViewChat = (await _authorizationService.AuthorizeAsync(_authenticationStateProviderUser, Permissions.Communication.Chat)).Succeeded;
        _canViewProducts = (await _authorizationService.AuthorizeAsync(_authenticationStateProviderUser, Permissions.Products.View)).Succeeded;
        _canViewBrands = (await _authorizationService.AuthorizeAsync(_authenticationStateProviderUser, Permissions.Brands.View)).Succeeded;
    }
}